package com.scholarship.modules.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scholarship.common.entity.RedisKeys;
import com.scholarship.modules.admin.dto.RankingListDto;
import com.scholarship.modules.admin.dto.RankingQueryDto;
import com.scholarship.common.dto.ResultListDto;
import com.scholarship.modules.admin.mapper.ARewardFeedbackMapper;
import com.scholarship.modules.admin.mapper.AUserInfoMapper;
import com.scholarship.modules.admin.service.RankingListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @Author Z.J.H
 * @create 2021/6/14 20:22
 */
@Service
public class RankingListServiceImpl implements RankingListService {

    @Autowired
    private AUserInfoMapper aUserInfoMapper;

    @Autowired
    private ARewardFeedbackMapper aRewardFeedbackMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Map<String, List<String>> getRankingQuery() throws Exception {

        String redisKey = RedisKeys.RANKING_QUERY.toString();
        //查询缓存
        Map<String, List<String>> map = (Map<String, List<String>>) redisTemplate.opsForValue().get(redisKey);
        //双重检测锁
        if(map == null) {
            synchronized (this) {
                //再次查询缓存
                map = (Map<String, List<String>>) redisTemplate.opsForValue().get(redisKey);
                //缓存为空，则从数据库获取数据
                if(map == null) {
                    List<String> years = new ArrayList<>();
                    years.add("所有学年");
                    years.addAll(aUserInfoMapper.listYear());

                    List<String> majors = new ArrayList<>();
                    majors.add("所有专业");
                    majors.addAll(aUserInfoMapper.listMajor());

                    List<String> stuClasses = new ArrayList<>();
                    stuClasses.add("所有班级");
                    stuClasses.addAll(aUserInfoMapper.listStuClass());

                    map = new HashMap<>();
                    map.put("year",years);
                    map.put("major",majors);
                    map.put("stuClass",stuClasses);
                    //查询数据放入缓存
                    redisTemplate.opsForValue().set(redisKey,map,30L, TimeUnit.SECONDS);
                }
            }
        }

        return map;
    }

    /**
     * 查询所有排名信息
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public ResultListDto<RankingListDto> listAllRanking(Integer pageNum, Integer pageSize) throws Exception {
        //使用PageHelper实现分页
        PageHelper.startPage(pageNum,pageSize);
        List<RankingListDto> list = aUserInfoMapper.listRankingListDto();
        PageInfo<RankingListDto> pageInfo = new PageInfo<>(list);

        return new ResultListDto<RankingListDto>(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 根据查询条件查询排名信息
     * @param pageNum
     * @param pageSize
     * @param rankingQueryDto
     * @return
     * @throws Exception
     */
    @Override
    public ResultListDto<RankingListDto> listRankingByQuery(Integer pageNum, Integer pageSize, RankingQueryDto rankingQueryDto) throws Exception {
        //使用PageHelper实现分页
        PageHelper.startPage(pageNum,pageSize);
        List<RankingListDto> list = aUserInfoMapper.listRankingListDtoByQuery(rankingQueryDto);
        PageInfo<RankingListDto> pageInfo = new PageInfo<>(list);

        return new ResultListDto<RankingListDto>(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 根据搜索查询排名信息
     * @param pageNum
     * @param pageSize
     * @param searchStr
     * @return
     * @throws Exception
     */
    @Override
    public ResultListDto<RankingListDto> listRankingBySearch(Integer pageNum, Integer pageSize, String searchStr) throws Exception {
        //使用PageHelper实现分页
        PageHelper.startPage(pageNum,pageSize);
        List<RankingListDto> list = aUserInfoMapper.listRankingListDtoBySearch(searchStr);
        PageInfo<RankingListDto> pageInfo = new PageInfo<>(list);

        return new ResultListDto<RankingListDto>(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 即刻刷新排名信息
     * @throws Exception
     */
    @Override
    public void refreshRankingInformation() throws Exception {
        //获取导入年份
        Calendar calendar = Calendar.getInstance();
        String importYear = String.valueOf(calendar.get(Calendar.YEAR));
        //更新总申请状态,已申请奖学金学生的总成绩和总排名
        aRewardFeedbackMapper.updateStatesByBarringApplicationState(importYear,3,1);
        aUserInfoMapper.updateTotalScore();
        aUserInfoMapper.updateTotalRank();
    }
}
